package com.lpb.spring.lpbspring.getway.model;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.lpb.spring.lpbspring.getway.helper.JSONHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RestModel implements Serializable {
    private String code;
    private String describe;
    private Object data;
    private boolean success;

    public RestModel() {
        this.data = new JSONObject();
    }

    public RestModel(Information information) {
        this();
        this.setInformation(information);
    }

    public RestModel(Object data) {
        this.data = data;
    }

    public RestModel(String key, Object value) {
        this.data = JSONHelper.createObject(key, value);
    }

    public void add(Object value) {
        if (!(this.data instanceof List)) {
            this.data = new ArrayList<>();
        }
        ((List) this.data).add(value);
    }

    public void put(String key, Object value) {
        if (!(this.data instanceof Map)) {
            this.data = new JSONObject();
        }
        ((Map) this.data).put(key, value);
    }

    public String getDescribe() {
        return this.describe;
    }

    public static Success success() {
        return new Success();
    }

    public static Success success(Object data) {
        return new Success(data);
    }

    public static Success success(String key, Object value) {
        return new Success(key, value);
    }

    public static Fail fail(Information information) {
        return new Fail(information);
    }

    public static Fail fail(String describe) {
        return new Fail(describe);
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    public void setInformation(String code) {
        this.code = code;
    }

    @JSONField(deserialize = false)
    public void setInformation(Information information) {
        this.code = information.toString();
        this.describe = information.getComment();
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

}
